﻿using UnityEngine;
using System.Collections;
using PlayerClasses;
using GeneralStorageClasses;

namespace GUIElements
{
	/// <summary>
	/// Squad view pane in the overworld.
	/// </summary>
	public class SquadView : MonoBehaviour
	{
		public GUIStyle squadStyle;
		public GUIStyle windowStyle;
		public GUIStyle buttonStyle;
		public GUIStyle labelStyle;

		public Camera squadCamera;

		//Mouse variables
		public float mouseOutDist = 200;
		private float mouseX, mouseY;

		//Window Position
		private Rect windowRect;
		private float width;
		private float height;

		//Camera rect
		private Rect cameraRect;

		//Squad View - button variables
		private bool squadView;
		private int svBtnH;

		//Squad View - box variables
		private float svBoxX = Screen.width / 2 - 320;
		private int svBoxH;

		//Character info box variables
		private bool characterInfoBox = false;

		//TODO: Make RECT
		private float ibX = Screen.width / 2 - 100, ibY = 430;
		private int ibW = 200, ibH = 200;

		//Array of characters and the current character for the info box
		private Squad squad;
		private int curCharacter = 0;

		void Awake ()
		{
			squad = new Squad ();
			squadView = true;

			//Fetch squad level
			//GetComponent<TextMesh>().text = getSquadLevel();

			//Init window size
			width = windowStyle.active.background.width;
			height = windowStyle.active.background.height;
		
			windowRect = new Rect (Screen.width / 2 - width / 2, Screen.height - height, width, height);
			cameraRect = new Rect (windowRect.x / Screen.width, - 0.01f, windowRect.width / Screen.width, windowRect.height / Screen.height);

			svBtnH = buttonStyle.active.background.height;

			//Check if GameManager is there if not it will create it self
			if (GameManager.Instance != null) {

			}

			
		}

		// Update is called once per frame
		void Update ()
		{
			//Debug.Log("Squad: " + squadView + ", CIB: " + characterInfoBox);

			if (Input.mousePosition.x < mouseX - mouseOutDist || Input.mousePosition.x > mouseX + mouseOutDist) {
				characterInfoBox = false;
			}

			if (Input.mousePosition.y < mouseY - mouseOutDist || Input.mousePosition.y > mouseY + mouseOutDist) {
				characterInfoBox = false;
			}
		}
	
		void OnGUI ()
		{
			GUI.Window (5, windowRect, windowContent, "", windowStyle);

			if (squadView) {
				//Character info box
				if (characterInfoBox) {
					GUI.Box (new Rect (ibX, ibY, ibW, ibH), "\n" + squad [curCharacter].Name +
						"\nLevel: " + squad [curCharacter].Level + 
						"\n\nHealth: " + squad [curCharacter].Health +
						"\nArmour: " + squad [curCharacter].Armour +
						"\nStamina: " + squad [curCharacter].Stamina + 
						"\nSpeed: " + squad [curCharacter].Speed + 
						"\nStrength: " + squad [curCharacter].Strength +
						"\nAgility: " + squad [curCharacter].Agility +
						"\nWeapon Proficiency: " + squad [curCharacter].WeaponProficiency +
						"\n3DMG Proficiency: " + squad [curCharacter].TdmgProficiency);
				}
			}
		}
	
		/// <summary>
		/// Contains the contents of the window.
		/// </summary>
		/// <param name="windowID">Window I.</param>
		private void windowContent (int windowID)
		{
			//Squad View Button
			if (GUI.Button (new Rect (0, 0, windowRect.width, svBtnH), "", buttonStyle)) {
				squadView = !squadView;
			}
		
			if (squadView == true) {	
				windowRect.y = Screen.height - height;
				squadCamera.camera.rect = cameraRect;

				GUI.Label (new Rect (windowRect.width / 2 - 3, buttonStyle.active.background.height - 2, 20, 20), squad.Level.ToString (), labelStyle);

				GUILayout.BeginArea (new Rect (24, 40, 450, 150));
				GUILayout.BeginHorizontal ();
			
		
				//Character buttons to reveal character info box
				for (int i = 0; i < 4; i++) {
					drawCharacterFrame (i);
				}

				GUILayout.EndHorizontal ();
				GUILayout.EndArea ();
			} else {
				windowRect.y = Screen.height - buttonStyle.active.background.height;
				squadCamera.camera.rect = new Rect (cameraRect.x, cameraRect.y - 1.0f, cameraRect.width, cameraRect.height);
			}
		}

		//Used to get the mouse position if a character button was pressed
		/// <summary>
		/// Gets the mouse position clicked.
		/// </summary>
		public void getMousePosClicked ()
		{
			characterInfoBox = true;
			mouseX = Input.mousePosition.x;
			mouseY = Input.mousePosition.y;
		}

		//Initialises character stats/info
		/// <summary>
		/// Gets the character info.
		/// </summary>
		/// <param name="count">Count.</param>
		public void getCharacterInfo (int count)
		{
			//Grab character stats
		}

		/// <summary>
		/// Draws the character frame.
		/// </summary>
		/// <param name="curCharacter">Current character.</param>
		public void drawCharacterFrame (int curCharacter)
		{
			GUILayoutOption btnW = GUILayout.Width (squadStyle.active.background.width);
			GUILayoutOption btnH = GUILayout.Height (squadStyle.active.background.height);
		
			if (GUILayout.Button ("", squadStyle, btnW, btnH)) {
				getMousePosClicked ();
			}
			GUILayout.Space (16);
		}
	}
}
